#include "Timer_PWM.h"

void tmr1_pwm_config(void)
{
    tmr_output_config_type tmr_oc_init_structure;
    tmr_brkdt_config_type tmr_break_deadtime_init_structure;
    
    // 使能TMR1时钟
    crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE);
    
    // 配置GPIO
    gpio_init_type gpio_init_struct;
    gpio_default_para_init(&gpio_init_struct);
    
    // 配置高侧输出引脚 PA8-10
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
    gpio_init_struct.gpio_pins = GPIO_PINS_8 | GPIO_PINS_9 | GPIO_PINS_10;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init(GPIOA, &gpio_init_struct);
    
    // 配置低侧输出引脚 PB13-15
    gpio_init_struct.gpio_pins = GPIO_PINS_13 | GPIO_PINS_14 | GPIO_PINS_15;
    gpio_init(GPIOB, &gpio_init_struct);
    
    // 配置TMR1基本参数
    tmr_base_init(TMR1, SystemCoreClock / 15000 - 1, 0);  // 设置周期为1000，预分频为0
    tmr_cnt_dir_set(TMR1, TMR_COUNT_UP);  // 设置为中心对齐模式2
    
    // 配置输出结构体
    tmr_output_default_para_init(&tmr_oc_init_structure);
    
		/* channel 1, 2, 3 and 4 configuration in output mode */
		tmr_output_default_para_init(&tmr_oc_init_structure);
		tmr_oc_init_structure.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_B;
		tmr_oc_init_structure.oc_output_state = TRUE;
		tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_LOW;
		tmr_oc_init_structure.oc_idle_state = TRUE;
		tmr_oc_init_structure.occ_output_state = TRUE;
		tmr_oc_init_structure.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH;
		tmr_oc_init_structure.occ_idle_state = FALSE;
    
    // 配置通道1（高侧PA8，低侧PB13）
    tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_1, &tmr_oc_init_structure);
    
    // 配置通道2（高侧PA9，低侧PB14）
    tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_2, &tmr_oc_init_structure);
    
    // 配置通道3（高侧PA10，低侧PB15）
    tmr_output_channel_config(TMR1, TMR_SELECT_CHANNEL_3, &tmr_oc_init_structure);
    
    // 配置死区时间
    tmr_brkdt_default_para_init(&tmr_break_deadtime_init_structure);
    tmr_break_deadtime_init_structure.deadtime = 100;  // 设置死区时间
    tmr_break_deadtime_init_structure.brk_enable = TRUE;
    tmr_break_deadtime_init_structure.brk_polarity = TMR_BRK_INPUT_ACTIVE_LOW;
    tmr_break_deadtime_init_structure.auto_output_enable = TRUE;
    tmr_brkdt_config(TMR1, &tmr_break_deadtime_init_structure);
    
    // 启用通道缓冲
    tmr_channel_buffer_enable(TMR1, TRUE);
    
    // 启用TMR1输出
    tmr_output_enable(TMR1, TRUE);
    
    // 启用TMR1计数器
    tmr_counter_enable(TMR1, TRUE);
}

// 设置PWM占空比
void set_pwm_duty(uint8_t channel, float duty)
{
	if(duty > 1.0f)
	{
		duty = 1.0f;
	}
	else if(duty < 0)
	{
		duty = 0;
	}
	uint16_t set_pwm = MAX_Timer * duty;
    switch(channel)
    {
        case 1:
            tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, set_pwm);
            break;
        case 2:
            tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, set_pwm);
            break;
        case 3:
            tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_3, set_pwm);
            break;
    }
}



void motor_pwm_set(uint8_t motor_ch , float ch_pwm_set)
{
	switch(motor_ch)
	{
		case 1: tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_1, (uint32_t)(ch_pwm_set * SystemCoreClock / 15000) -1);break;
		case 2: tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_2, (uint32_t)(ch_pwm_set * SystemCoreClock / 15000) -1);break;
		case 3: tmr_channel_value_set(TMR1, TMR_SELECT_CHANNEL_3, (uint32_t)(ch_pwm_set * SystemCoreClock / 15000) -1);break;
	
	}
}